home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1126 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  101 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Array of pointers to a tree structure ?
  5. Date: 11 Jan 1996 16:40:35 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4d3ei3$9fl@news.iag.net>
  8. References: <4d067t$9lc@hermes.fundp.ac.be>
  9. NNTP-Posting-Host: pm3-orl4.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4d067t$9lc@hermes.fundp.ac.be>, fmelo@biq.fundp.ac.be says...
  13. >
  14. >Hi Colleagues, I have been learning C language just from one month ago 
  15. >and I don't know too much about this language. I have a problem with the 
  16. >assignment of pointers in an array of pointers with a specifical defined 
  17. >structure. For example:
  18. >
  19. >
  20. >/* BOXES STRUCTURE FOR THE CONSTRUCTION OF A TREE */
  21. >
  22. >struct boxes {
  23. >              int number;
  24. >              struct boxes *pointer_1;
  25. >              struct boxes *pointer_2;
  26. >              struct boxes *pointer_3;
  27. >             }
  28. >
  29. >/* ARRAY OF POINTERS TO BOXES STRUCTURE FOR MANAGE */
  30. >/* SOME BRANCHES OF THE TREE                       */
  31. >
  32. >struct boxes array_pointers [15];
  33. >
  34. >
  35. >Supose that I have the array of pointers already defined, with every 
  36. >pointer pointing to a boxes structure and every boxes structure 
  37. >contains only the integer number defined. In this moment I would like 
  38. >to define the three pointers in every box in the way of these pointers 
  39. >point to another element in the array. I have tried statements like 
  40. >this with out success:
  41. >
  42. >
  43. >(*(array_pointers + 1)).pointer_1 = (array_pointers + 0);
  44. >
  45. >or
  46. >
  47. >(array_pointers[1]).pointer_1 = &array_pointers[0];
  48. >                                 
  49. >
  50. >Somebody know how can I define a pointer inside the box in the way that 
  51. >it points to a box in the array ???, In advance, thank you very much, 
  52. >best wishes,        
  53.  
  54. Here are some variations.  One of them should allow you to accomplish your 
  55. objective. I prefer index notation to dereference notation, but feel free 
  56. to use whichever you happen to like.  The compiler will normally generate
  57. the same code for both.
  58.  
  59. #include <stdio.h>
  60.  
  61. struct boxes {
  62.               int number;
  63.               struct boxes *pointer_1;
  64.               struct boxes *pointer_2;
  65.               struct boxes *pointer_3;
  66.              };
  67.  
  68. int main()
  69.    {  
  70.    int i;
  71.    struct boxes array_structs[5];   /* array of struct boxes */
  72.    struct boxes *array_pointers[5]; /* array of pointers to struct boxes */
  73.    
  74.    array_pointers[0] = array_structs;   
  75.    array_pointers[1] = array_structs+1;   
  76.    array_pointers[2] = array_structs+2;   
  77.    array_pointers[3] = array_structs+3;   
  78.    array_pointers[4] = &array_structs[4];   
  79.    
  80.    array_structs[0].number = 1;
  81.    (*(array_pointers+1))->number = 2;
  82.    
  83.    array_structs[0].pointer_1 = &array_structs[2];
  84.    array_pointers[0]->pointer_2 = &array_structs[3];
  85.    array_pointers[0]->pointer_3 = array_structs + 4;
  86.     
  87.    array_structs[0].pointer_1->number = 3;
  88.    array_pointers[0]->pointer_2->number = 4;
  89.    array_pointers[0]->pointer_3->number = 5;
  90.    
  91.    for( i = 0; i < 5; i++)
  92.       printf( "array_structs%d].number = %d\n", i, array_structs[i]);
  93.       
  94.    return (0);
  95.    }
  96.  
  97. -- 
  98. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  99. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  100.  
  101.